package com.jereh.weibo.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.TextView;

import com.jereh.weibo.R;
import com.jereh.weibo.fragment.AddFragment;
import com.jereh.weibo.fragment.HomeFragment;
import com.jereh.weibo.fragment.MessageFragment;
import com.jereh.weibo.fragment.SearchFragment;
import com.jereh.weibo.fragment.UserCenterFragment;

public class MainActivity extends AppCompatActivity {

    private FragmentTabHost fragmentTabHost;
    private String[] titles={"首页","消息","","发现","我"};
    private int imageButton[] = { R.drawable.home_main,
            R.drawable.home_message,
            R.drawable.home_compose,
            R.drawable.home_search,
            R.drawable.home_mine };
    private Class fragmentArray[] = {HomeFragment.class,MessageFragment.class,AddFragment.class,
            SearchFragment.class,UserCenterFragment.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initFragmentTabHost();

    }

    private void initFragmentTabHost(){
// 实例化tabhost
        fragmentTabHost = (FragmentTabHost) findViewById(R.id.tabhost);
        fragmentTabHost.setup(this, getSupportFragmentManager(),R.id.fragcontent);
        for (int i = 0; i < titles.length; i++) {
            TabHost.TabSpec spec=fragmentTabHost.newTabSpec(titles[i]).setIndicator(getView(i));
            fragmentTabHost.addTab(spec, fragmentArray[i], null);

        }
        ((TextView)fragmentTabHost.getTabWidget().getChildAt(0).findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.topOrange));

        fragmentTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
            @Override
            public void onTabChanged(String tabId) {
                fragmentTabHost.setCurrentTabByTag(tabId);
                for (int i = 0; i < fragmentTabHost.getTabWidget().getChildCount(); i++) {
                    View view = fragmentTabHost.getTabWidget().getChildAt(i);
                    if (fragmentTabHost.getCurrentTab() == i) {//选中
                        if(i!=2){
                            ((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.topOrange));
                        }
                    } else {
                        if (i!=2){
                            ((TextView) view.findViewById(R.id.title)).setTextColor(getResources().getColor(R.color.bottomTextGray));
                        }
                    }
                }

            }
        });
    }

    private View getView(int i) {
        int j = 0;
        View view=null;
        if(i!=2){
            view=View.inflate(MainActivity.this, R.layout.tabhost_content, null);
            ImageView imageView=(ImageView) view.findViewById(R.id.logo);
            TextView textView=(TextView) view.findViewById(R.id.title);
            imageView.setImageResource(imageButton[i]);
            textView.setText(titles[i]);
        }else {
            view=View.inflate(MainActivity.this, R.layout.tabhost_center, null);
            ImageView imageView=(ImageView) view.findViewById(R.id.logo_center);
            imageView.setImageResource(imageButton[2]);
        }
        return view;
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
    }
}
